package com.sdtv.mediunfusion.mvp.ui.punch;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.sdtv.basemodule.annotations.CreatePresenter;
import com.sdtv.basemodule.annotations.PresenterVariable;
import com.sdtv.basemodule.base.BaseActivity;
import com.sdtv.basemodule.bean.Results;
import com.sdtv.basemodule.utils.ClickProxy;
import com.sdtv.mediunfusion.R;
import com.sdtv.mediunfusion.bean.PunchBean;
import com.sdtv.mediunfusion.mvp.contract.MainContract;
import com.sdtv.mediunfusion.mvp.presenter.PunchPresenter;
import com.sdtv.mediunfusion.mvp.ui.home.HomeActivity;
import com.sdtv.mediunfusion.utils.RouterPath;

import java.util.List;

import butterknife.BindView;

@Route(path = RouterPath.TO_PUNCHREMARK_ACTIVITY)
@CreatePresenter(presenter = PunchPresenter.class)
public class PunchRemarkActivity extends BaseActivity implements MainContract.PunchView {


    @BindView(R.id.pr_address)
    TextView prAddress;
    @BindView(R.id.pr_reasonEdit)
    EditText prReasonEdit;
    @BindView(R.id.pr_sureButton)
    TextView prSureButton;
    @BindView(R.id.pr_cancelButton)
    TextView prCancelButton;


    //获取上个页面传递的值
    @Autowired
    String address;

    @Autowired
    String time;

    // 获取presenter对象
    @PresenterVariable
    PunchPresenter punchPresenter;

    /**
     * 设置布局文件
     *
     * @return
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_punch_remark;
    }

    /**
     * 初始化View
     */
    @Override
    public void initView() {
        // 设置地址
        prAddress.setText(address);
        //按钮点击事件
        prSureButton.setOnClickListener(clickProxy);
        prCancelButton.setOnClickListener(clickProxy);
    }


    private ClickProxy clickProxy = new ClickProxy(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.pr_sureButton:
                    // 提交备注内容
                    if (TextUtils.isEmpty(prReasonEdit.getText())) {
                        //提示输入内容
                        ToastUtils.showShort("请输入备注内容");
                        return;
                    }
                    punchPresenter.submitTime(address, "2", time, prReasonEdit.getText().toString());
                    break;
                case R.id.pr_cancelButton:
                    // 取消 --返回上级页面
                    onBackPressed();
                    break;
                default:
            }
        }
    });


    @Override
    protected void initStatusBar() {
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor(com.sdtv.basemodule.R.color.colorPrimary)
                //解决软键盘与底部输入框冲突问题-- 设置为false是强制不顶起底部view
                .keyboardEnable(false)
                .init();
    }

    /**
     * 加载页面数据
     */
    @Override
    public void initData() {

    }

    @Override
    public void setUserPunchInfo(List<PunchBean> loggingList) {

    }

    /**
     * 返回打卡结果
     *
     * @param response
     */
    @Override
    public void result(Results response) {
        String msg = response.getMsg();
        if (!TextUtils.isEmpty(msg)) {
            ToastUtils.showShort(msg);
        }
        String result = "success";
        if (!msg.contains("成功")) {
            result = "fail";
        }
        Intent intent = new Intent(this,
                HomeActivity.class);
        intent.putExtra("punch_result", result);
        setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * 打卡异常
     * 通常是网络原因 或者 接口错误等引起的
     */
    @Override
    public void punchFail() {
        ToastUtils.showShort(R.string.pubch_fail);
    }
}
